package com.tiejun.ge.http.service.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;
import com.tiejun.ge.http.config.ApiConfig;
import com.tiejun.ge.http.service.HttpService;
import com.tiejun.ge.http.util.HttpUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: springboot
 * @description: HttpService
 * @author: getiejun
 * @create: 2025-08-14 23:16
 **/
@Service
@Slf4j
public class HttpServiceImpl implements HttpService {

    @Resource
    private ApiConfig apiConfig;

    @Override
    public void getHelloWorld() {
        String url = apiConfig.getUrl() + ApiConfig.HELLO_WORLD_PATH;
        HttpRequest httpRequest = HttpRequest.get(url).header(HttpUtil.getMIMEHeader());
        HttpResponse httpResponse = httpRequest.execute();
        if(httpResponse.isOk()) {
            log.info("url:{}, response data is:{}", url, httpResponse.body());
        } else {
            log.error(">>HttpServiceImpl getHelloWorld method. The request error. the url is:{}", url);
        }
    }

    @Override
    public void getUserInfo() {
        String url = apiConfig.getUrl() + ApiConfig.USER_DETAIL;

        Map<String, Object> requestParam = new HashMap<>();
        requestParam.put("id", 1L);

        HttpRequest httpRequest = HttpRequest.post(url).header(HttpUtil.getMIMEHeader()).body(JSONUtil.toJsonStr(requestParam));
        HttpResponse httpResponse = httpRequest.execute();

        if(httpResponse.isOk()) {
            log.info("url:{}, response data is:{}", url, httpResponse.body());
        } else {
            log.error(">>HttpServiceImpl getUserInfo method. The request error. the url is:{}, request param is:{}", url, JSONUtil.toJsonStr(requestParam));
        }
    }
}
